package com.kyrie.controller;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.kyrie.annotation.TransLog;
import com.kyrie.dto.LoginLogDto;
import com.kyrie.system.druid.DatasourceSelectHelper;
import com.kyrie.system.mybatis.execption.ServiceException;
import com.kyrie.system.mybatis.service.IDefaultService;
import com.kyrie.utils.ConvertUtils;
import com.kyrie.utils.GlobalConstants;
import com.kyrie.vo.LoginLogVO;
import com.kyrie.vo.ResponseBean;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 描述：demo controller
 * @author wuxiang
 * @date 2020-04-19 19:38:00
 */
@Validated
@RestController
public class DemoController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource(name = "defaultService")
    private IDefaultService service;

    @TransLog(transCode = "/test",description = "@测试接口")
    @RequestMapping(value = "/test",method = RequestMethod.POST)
    @RequiresRoles(logical = Logical.OR, value = {"user","systemAdmin"})
    public ResponseBean<List<LoginLogVO>> getLog(@Valid @RequestBody LoginLogDto loginLogDto) throws ServiceException {

        logger.info("@demo示例开始执行：{}", JSON.toJSONString(loginLogDto));

        // 默认初始化的数据源
        List<LoginLogVO> loginLogVOInit = service.selectList(GlobalConstants.NAME_SPACE + "getLoginLog", loginLogDto);
        logger.info("初始化加载的数据源查询结果为：{}",JSON.toJSONString(loginLogVOInit));

        // 设置业务库
        DatasourceSelectHelper.setKey(DatasourceSelectHelper.BUSINESS_DB);
        List<LoginLogVO> loginLogVOTwo = service.selectList(GlobalConstants.NAME_SPACE + "getLoginLog", loginLogDto);
        logger.info("切换数据源local-2查询结果为：{}",JSON.toJSONString(loginLogVOTwo));


        // 设置配置库
        DatasourceSelectHelper.setKey(DatasourceSelectHelper.CONFIG_DB);
        List<LoginLogVO> loginLogVOOne = service.selectList(GlobalConstants.NAME_SPACE + "getLoginLog", loginLogDto);
        logger.info("切换数据源local-1查询结果为：{}",JSON.toJSONString(loginLogVOOne));

        return ResponseBean.success(loginLogVOOne, GlobalConstants.SUCCESS_CODE,"@登录日志查询接口请求成功");
    }
}
